package com.example.projectmanagement.repository;

import com.example.projectmanagement.model.OperationLog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface OperationLogRepository extends JpaRepository<OperationLog, Long>, JpaSpecificationExecutor<OperationLog> {
    
    // 根据用户名查询日志
    List<OperationLog> findByUsername(String username);
    
    // 根据操作类型查询日志
    List<OperationLog> findByOperationType(String operationType);
    
    // 根据时间范围查询日志
    List<OperationLog> findByTimestampBetween(LocalDateTime startTime, LocalDateTime endTime);
    
    // 根据用户名和操作类型查询日志
    List<OperationLog> findByUsernameAndOperationType(String username, String operationType);
    
    // 根据模块查询日志
    List<OperationLog> findByModule(String module);
    
    // 自定义查询：根据条件查询日志
    @Query("SELECT l FROM OperationLog l WHERE " +
           "(:username IS NULL OR l.username LIKE %:username%) AND " +
           "(:operationType IS NULL OR l.operationType = :operationType) AND " +
           "(:startTime IS NULL OR l.timestamp >= :startTime) AND " +
           "(:endTime IS NULL OR l.timestamp <= :endTime)")
    List<OperationLog> findByFilters(@Param("username") String username,
                                   @Param("operationType") String operationType,
                                   @Param("startTime") LocalDateTime startTime,
                                   @Param("endTime") LocalDateTime endTime);
}